<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>2048 Game</title>
    <style>
        table {
            border-collapse: collapse;
        }

        td {
            width: 50px;
            height: 50px;
            border: 1px solid black;
            text-align: center;
            font-size: 20px;
        }
    </style>
</head>

<body>
    <?php
    error_reporting(E_ALL & ~E_WARNING);
    session_start();

    // 初始化游戏棋盘
    if (!isset($_SESSION['board'])) {
        $_SESSION['board'] = array_fill(0, 4, array_fill(0, 4, 0));
        addRandomTile($_SESSION['board']);
        addRandomTile($_SESSION['board']);
    }

    // 处理移动请求
    if (isset($_GET['move'])) {
        $move = $_GET['move'];
        switch ($move) {
            case 'up':
                moveUp($_SESSION['board']);
                break;
            case 'down':
                moveDown($_SESSION['board']);
                break;
            case 'left':
                moveLeft($_SESSION['board']);
                break;
            case 'right':
                moveRight($_SESSION['board']);
                break;
        }
        addRandomTile($_SESSION['board']);
    }

    // 显示游戏棋盘
    echo '<table>';
    for ($i = 0; $i < 4; $i++) {
        echo '<tr>';
        for ($j = 0; $j < 4; $j++) {
            echo '<td>' . ($_SESSION['board'][$i][$j] > 0 ? $_SESSION['board'][$i][$j] : '') . '</td>';
        }
        echo '</tr>';
    }
    echo '</table>';

    // 移动按钮
    echo '<br>';
    echo '<button onclick="move(\'up\')">Up</button>';
    echo '<button onclick="move(\'down\')">Down</button>';
    echo '<button onclick="move(\'left\')">Left</button>';
    echo '<button onclick="move(\'right\')">Right</button>';

    // JavaScript 函数，用于发送移动请求
    echo '<script>
        function move(direction) {
            window.location.href = "index.php?move=" + direction;
        }
    </script>';

    // 随机生成新数字
    function addRandomTile(&$board) {
        $emptyCells = [];
        for ($i = 0; $i < 4; $i++) {
            for ($j = 0; $j < 4; $j++) {
                if ($board[$i][$j] == 0) {
                    $emptyCells[] = [$i, $j];
                }
            }
        }
        if (!empty($emptyCells)) {
            $randomCell = $emptyCells[array_rand($emptyCells)];
            $board[$randomCell[0]][$randomCell[1]] = rand(0, 1) == 0 ? 2 : 4;
        }
    }

    // 向上移动
    function moveUp(&$board) {
        for ($j = 0; $j < 4; $j++) {
            $column = [];
            for ($i = 0; $i < 4; $i++) {
                if ($board[$i][$j] > 0) {
                    $column[] = $board[$i][$j];
                }
            }
            $column = merge($column);
            $column = array_pad($column, 4, 0);
            for ($i = 0; $i < 4; $i++) {
                $board[$i][$j] = $column[$i];
            }
        }
    }

    // 向下移动
    function moveDown(&$board) {
        for ($j = 0; $j < 4; $j++) {
            $column = [];
            for ($i = 3; $i >= 0; $i--) {
                if ($board[$i][$j] > 0) {
                    $column[] = $board[$i][$j];
                }
            }
            $column = merge($column);
            $column = array_pad($column, 4, 0);
            for ($i = 3; $i >= 0; $i--) {
                $board[$i][$j] = $column[3 - $i];
            }
        }
    }

    // 向左移动
    function moveLeft(&$board) {
        for ($i = 0; $i < 4; $i++) {
            $row = array_filter($board[$i]);
            $row = merge($row);
            $row = array_pad($row, 4, 0);
            $board[$i] = $row;
        }
    }

    // 向右移动
    function moveRight(&$board) {
        for ($i = 0; $i < 4; $i++) {
            $row = array_reverse(array_filter($board[$i]));
            $row = merge($row);
            $row = array_pad($row, 4, 0);
            $board[$i] = array_reverse($row);
        }
    }

    // 合并相同数字
    function merge($arr) {
        for ($i = 0; $i < count($arr) - 1; $i++) {
            if ($arr[$i] == $arr[$i + 1]) {
                $arr[$i] *= 2;
                unset($arr[$i + 1]);
                $arr = array_values($arr);
            }
        }
        return $arr;
    }
    ?>
</body>

</html>